package com.css.fxfzaqbz.modules.assess.service.impl;

import com.css.fxfzaqbz.constants.YNEnum;
import com.css.fxfzaqbz.modules.assess.entity.EarthEntity;
import com.css.fxfzaqbz.modules.assess.repository.EarthEntityRepository;
import com.css.fxfzaqbz.modules.assess.repository.RiskAssessmentRepository;
import com.css.fxfzaqbz.modules.assess.service.EarthEntityService;
import com.css.fxfzaqbz.modules.data.entity.ImportantActivity;
import com.css.fxfzaqbz.modules.data.service.ImportantActivityService;
import com.css.fxfzaqbz.util.PlatformDateUtils;
import com.css.fxfzaqbz.util.PlatformSessionUtils;
import com.css.fxfzaqbz.util.UUIDGenerator;
import com.css.fxfzaqbz.web.PlatformSessionContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;


/**
 * @author y1fan
 * @create 2022-09-16 14:36
 */
@Service
public class EarthEntityServiceImpl implements EarthEntityService {


    @Resource
    private EarthEntityRepository earthEntityRepository;

    @Resource
    private ImportantActivityService importantActivityService;

    @Resource
    private RiskAssessmentRepository riskAssessmentRepository;

    @Override
    @Transactional
    public void saveEntity(EarthEntity earthEntity) {
        earthEntity.setEeCreateTime(PlatformDateUtils.getCurrentTimestamp());
        earthEntity.setEeDelFlag(YNEnum.N.toString());
        String userID = PlatformSessionContext.getUserID();
        earthEntity.setEeCreateUser(userID);
        earthEntityRepository.saveEntity(earthEntity);


    }

    @Override
    public EarthEntity getEarthByActiveId(String sraRelation) {
        EarthEntity earthByActiveId = earthEntityRepository.getEarthByActiveId(sraRelation);
        if (earthByActiveId != null) {
            earthByActiveId.setEeActiveId(sraRelation);
            earthByActiveId.setEeOpinionStatus(importantActivityService.getImportantActivityById(sraRelation).getIaOpinionStatus());
            earthByActiveId.setEeOpinion(importantActivityService.getImportantActivityById(sraRelation).getIaOpinion());
            earthByActiveId.setEeEntities(riskAssessmentRepository.getAllRiskAssessment(sraRelation));

            return earthByActiveId;
        } else {
            EarthEntity earthEntity = new EarthEntity();
            earthEntity.setEeEntities(riskAssessmentRepository.getAllRiskAssessment(sraRelation));
            earthEntity.setEeOpinionStatus(importantActivityService.getImportantActivityById(sraRelation).getIaOpinionStatus());
            earthEntity.setEeOpinion(importantActivityService.getImportantActivityById(sraRelation).getIaOpinion());
            return earthEntity;
        }
    }

    @Override
    public void saveTask(ImportantActivity importantActivity1) {
        EarthEntity earthEntity1 = earthEntityRepository.getEarthByActiveId(importantActivity1.getIaId());
        if (earthEntity1 == null) {
            EarthEntity earthEntity = new EarthEntity();
            earthEntity.setEeId(UUIDGenerator.getUUID());
            earthEntity.setContent("地震危险性评估");
            earthEntity.setEeStatus(YNEnum.N.toString());
            earthEntity.setEeActiveId(importantActivity1.getIaId());
            earthEntity.setEeDelFlag(YNEnum.N.toString());
            earthEntity.setEeCreateTime(PlatformDateUtils.getCurrentTimestamp());
            earthEntity.setEeCreateUser(PlatformSessionUtils.getUserId());
            earthEntity.setEeOpinion(importantActivity1.getIaOpinion());
            earthEntityRepository.saveEntity(earthEntity);
        }

    }

    @Override
    public void upStatus(String id) {
        earthEntityRepository.upStatus(id);
    }
}
